package cn.imvc.app.models;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.ArticleClass;
import cn.imvc.app.entities.Department;
import cn.imvc.app.entities.Department_position;
import cn.imvc.app.entities.Position;
import cn.imvc.app.entities.Resource;
import cn.imvc.app.repositories.ArticleClassRepository;
import cn.imvc.core.HttpContext;
import cn.imvc.core.ICache;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author wjx
 * @time 2016年7月15日 下午5:05:08 
 *
 */
@Service
public class ArticleClassModelImpl implements ArticleClassModel {

	@Autowired
	ArticleClassRepository articleClassRepository;
	@Autowired
	RefRelationModel refRelationModel;
    @Autowired
    ICache cache;
    
	public LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getRequestMap() {
		return null;
	}

	public Page<ArticleClass> find(ViewRequest viewRequest) {
		return articleClassRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public ArticleClass get(int id) {
		return articleClassRepository.findOne(id);
	}

	public String delete(int id) {
		String result = refRelationModel.deleteDateCheck("Article_Class", "id", id);
		if(result.equals("")){
			articleClassRepository.delete(id);
			return "";
		}else{
			return result;
		}
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}

	public ArticleClass save(int id,ArticleClass articleClass) {
		articleClass.setParent(get(id));
		ArticleClass articleClassTmp = articleClassRepository.saveAndFlush(articleClass);
		cache.removeSecond("MAX_ID", "articleClass");
		return articleClassTmp;
	}

	public ArticleClass update(ArticleClass resource, int classId) {
		ArticleClass theArticleClass = articleClassRepository.getOne(classId);
		BeanUtils.copyProperties(resource, theArticleClass, new String[] { "id", "parent" });
		return articleClassRepository.saveAndFlush(theArticleClass);
	}

	public String getArticleClasses() {
		String rootUrl = HttpContext.getContext().getAttribute("rootUrl").toString();
		Set<ArticleClass> articleClassSet = articleClassRepository.findByParentIsNull();
		StringBuffer tree = new StringBuffer();
		for (ArticleClass articleClass : articleClassSet) {
			tree.append("<li class='treeli' data_id='" + articleClass.getId() + "'><a href='" + rootUrl + "/info/article-class/" + articleClass.getId() + "/edit' target='ajax' rel='jbsxBox'>"
					+ articleClass.getName() + "</a>");
			tree.append(this.getChildArticleClass(articleClass.getId(), rootUrl));
			tree.append("</li>");
		}
		if(tree.toString().equals("")){
			tree.append("<li class='treeli' data_id='0'><a href='" + rootUrl + "/info/article-class/0/edit' target='ajax' rel='jbsxBox'>文章类别</a></li>");
		}
		return tree.toString();
	}

	private String getChildArticleClass(int id, String rootUrl) {
		Set<ArticleClass> childArticleClasses = articleClassRepository.findByParentId(id);
		StringBuffer childNode = new StringBuffer();
		if (childArticleClasses.size() > 0) {
			childNode.append("<ul>");
			for (ArticleClass ac : childArticleClasses) {
				childNode.append("<li class='treeli' data_id='" + ac.getId() + "'><a href='" + rootUrl + "/info/article-class/" + ac.getId() + "/edit' target='ajax' rel='jbsxBox'>"
						+ ac.getName() + "</a>");
				childNode.append(this.getChildArticleClass(ac.getId(), rootUrl));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}

		return childNode.toString();

	}

	public String treeLookup() {
		Set<ArticleClass> articleClassSet = articleClassRepository.findByParentIsNull();
		StringBuffer tree = new StringBuffer();
		for (ArticleClass articleClass : articleClassSet) {
			tree.append("<li><a href=\"javascript:\" onclick=\"$.bringBack({id:'"+articleClass.getId()+"', name:'"+articleClass.getName()+"'})\">"+articleClass.getName()+"</a>");
			tree.append(this.getChildDep(articleClass.getId(),articleClass));
			tree.append("</li>");
		}
		return tree.toString();
	}
	
	private String getChildDep(int id,ArticleClass articleClass){
		Set<ArticleClass> childArticleClasses  =  articleClassRepository.findByParentId(id);
		StringBuffer childNode = new StringBuffer();
		if (childArticleClasses.size() > 0) {
			childNode.append("<ul>");
			for (ArticleClass ac : childArticleClasses) {
				childNode.append("<li><a href=\"javascript:\" onclick=\"$.bringBack({id:'"+ac.getId()+"', name:'"+ac.getName()+"'})\">"+ac.getName()+"</a>");
				childNode.append(this.getChildDep(ac.getId(),ac));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}
		return childNode.toString();
	}
}
